module UnitSize

import Relation;
import Set;
import util::Math;
import LinesOfCode;
import lang::java::jdt::JDT;
import lang::java::jdt::JavaADT;
import IO;

public rel[loc, set[int]] ExtractInformation(list[loc] files)
{
	return aux = {<f, ExtractFileInformation(f)> | loc f <- files};
}

//returns a list of all methods foreach Java File of project
public list[loc] GetAllJavaMethodsFromFiles(str project)
{
	list[loc] files = GetAllJavaFiles(project);
	return ([] | it + GetListMethodsFromFile(f)| loc f <- files);
}
//returns a list of all methods from a java file
public list[loc] GetListMethodsFromFile(loc file)
{
	fm = extractClass(file);
	return toList(domain(fm@methods));
}


public set[int] ExtractFileInformation(loc location)
{
	fm = extractClass(location);
	return aux = {LinesOfCode(method) | <method, body> <- fm@methods};
}

// Returns lines of code for list of methods
public rel[loc, int] CountLinesOfCodeInMethods(list[loc] methods)
{
	return aux = {<f, LinesOfCode(f)> | loc f <- methods};
}


public str GetUnitSizeScore(list[loc] files)
{
 	int lowRisk = 0, moderateRisk = 0, highRisk = 0, veryHighRisk = 0, totalRisk = 0;
	
	for(methodLOC <- range(ExtractInformation(files)))
	{
		for(testtest <- methodLOC)
		{
			if(testtest < 10)
				lowRisk += 1;
			else if (testtest < 50)
				moderateRisk += 1;
			else if (testtest < 100)
				highRisk += 1;
			else
				veryHighRisk += 1;
			totalRisk += 1;
		}
	}	
	
	lowRisk = percent(lowRisk, totalRisk);
	moderateRisk = percent(moderateRisk, totalRisk);
	highRisk = percent(highRisk, totalRisk);
	veryHighRisk = percent(veryHighRisk, totalRisk);
	
	str operator = "";
	if(moderateRisk <= 25 && highRisk == 0 && veryHighRisk == 0)
		operator = "++";
	else if(moderateRisk <= 30 && highRisk <= 5 && veryHighRisk == 0)
		operator = "+";
	else if(moderateRisk <= 40 && highRisk <= 10 && veryHighRisk == 0)
		operator = "o";
	else if(moderateRisk <= 50 && highRisk <= 15 && veryHighRisk <= 5)
		operator = "-";
	else
		operator = "--";
	
	return operator;
}